﻿/*************************************************************************************
 * 文 件 名:   HK_TipsCtrl
 * 
 * 描    述:  Tips工具总控脚本
 * 
 * 版    本：  V1.0
 * 创 建 者：  京产肠饭
 * 创建时间：  2022/2/27
 * 链    接：  https://gitee.com/JingChanChangFan/hk_-unity-tools
*************************************************************************************/

using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 标签控制管理
/// </summary>
public class HK_TipsCtrl : MonoBehaviour
{
    [Header("当前Tip的名称：")] public string tipName;
    [Header("是否可点击：")] public bool isCanClick;
    [Header("是否可缩放："), SerializeField] bool isCanAutoScale;

    [Header("默认摄像机距离："), SerializeField] Vector2 camRange;
    float defaultCamDis;

    [Header("缩放最小值："), SerializeField, Range(0, 1)] float minimum = 0.5f;
    [Header("缩放最大值："), SerializeField, Range(1, 2)] float maximum = 2f;
    float initScale;

    // 提示线
    Transform line_TF;
    // 提示标签物体
    Transform title_TF;

    [Header("Title标签向上的偏移值："), SerializeField] float titleOffSet = 0.4f;

    List<Transform> linePos_List = new List<Transform>();
    LineRenderer lineRenderer;

    void Awake()
    {
        line_TF = transform.Find("Line");
        title_TF = transform.Find("Title");
        lineRenderer = line_TF.GetComponent<LineRenderer>();
    }

    void Start()
    {
        ShowLineRenderer();
        title_TF.position = linePos_List[linePos_List.Count - 1].position + new Vector3(0, titleOffSet, 0);

        defaultCamDis = (camRange.y + camRange.x) / 2f;
        initScale = transform.localScale.x;
    }

#if UNITY_EDITOR
    void OnValidate() // 当Inspector中 isCanClick参数变化时，实时调整Title子物体是否可点击
    {
        if (title_TF != null)
            title_TF.GetComponent<HK_TipTitle>().SetCollider(isCanClick);
    }
#endif

    void Update()
    {
        //标签旋转
        title_TF.LookAt(new Vector3(Camera.main.transform.position.x, title_TF.position.y, Camera.main.transform.position.z));

        if (isCanAutoScale)
            SetTipScale();
    }

    float index = 0;
    float camDis = 0;

    // 根据摄像机距离设置Tip整体的缩放
    void SetTipScale()
    {
        camDis = Vector3.Distance(transform.position, Camera.main.transform.position);

        if (camDis < defaultCamDis)
        {
            if (camDis - camRange.x >= 0)
                index = (camDis - camRange.x) / (defaultCamDis - camRange.x) - 1;
            else
                index = -1;
        }
        else if (camDis > defaultCamDis)
        {
            if (camRange.y - camDis >= 0)
                index = 1 - (camRange.y - camDis) / (camRange.y - defaultCamDis);
            else
                index = 1;
        }
        else
        {
            index = 0;
        }

        // 0-1
        index = (index + 1) / 2f * (maximum - minimum) + minimum;
        transform.localScale = initScale * Vector3.one * index;
    }

    //显示画线
    void ShowLineRenderer()
    {
        foreach (Transform child_TF in line_TF)
        {
            linePos_List.Add(child_TF);
        }

        Vector3[] linePoses = new Vector3[linePos_List.Count];

        for (int i = 0; i < linePos_List.Count; i++)
        {
            linePoses[i] = linePos_List[i].localPosition;
        }

        lineRenderer.positionCount = linePos_List.Count;
        lineRenderer.SetPositions(linePoses);
    }

    /// <summary>
    /// 外界调用，当前Tip是否可点击
    /// </summary>
    /// <param name="isCanClick"></param>
    public void SetIsCanClick(bool _isCanClick)
    {
        isCanClick = _isCanClick;
        title_TF.GetComponent<HK_TipTitle>().SetCollider(_isCanClick);
    }

    /// <summary>
    /// 当前标签点击时调用
    /// </summary>
    public void CurrentTipClick()
    {
        Debug.Log(tipName + " 被点击");
    }
}
